package com.ete.service.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.Gravity
import androidx.annotation.ColorRes
import androidx.appcompat.widget.AppCompatTextView
import androidx.databinding.BindingAdapter
import com.blankj.utilcode.util.ColorUtils
import com.ete.service.service.R

/**
 * Author:LiXian
 * <p>
 * Desc:带圆角的按钮
 * Version:1.0
 * date 2024/1/9 15:28
 */
class EteTextView(context: Context, attr: AttributeSet?, defStyleAttr: Int) :
        AppCompatTextView(context, attr, defStyleAttr) {

    private var mPaint: Paint
    private val mBgRectF: RectF = RectF()

    private var mThemeColor: Int = 0
    private var mCorner: Float = 0F

    private var mStrokeWidth: Float = 0F
    private var mStrokeColor: Int = 0

    companion object {
        private const val DEFAULT_BG_CORNER = 9F //dp

        @JvmStatic
        @BindingAdapter(value = ["solidColor"])
        fun setTvSolidColor(mEteTv: EteTextView, solidColor: Int) {

        }
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.EteTextview)
            mCorner = typedArray.getDimension(R.styleable.EteTextview_ete_corner, DEFAULT_BG_CORNER)
            mThemeColor = typedArray.getColor(R.styleable.EteTextview_ete_solid, 0)
            mStrokeWidth = typedArray.getDimension(R.styleable.EteTextview_ete_stroke_width, 0F)
            mStrokeColor = typedArray.getColor(R.styleable.EteTextview_ete_stroke_color, Color.BLACK)
            typedArray.recycle()
        }
        if (mThemeColor != 0) {
            mPaint.color = mThemeColor
        }
    }

    init {
        gravity = Gravity.CENTER
        mPaint = Paint().apply {
            style = Paint.Style.FILL
        }
    }

    fun updateThemeColor(@ColorRes mColor: Int) {
        mPaint.color = ColorUtils.getColor(mColor)
        invalidate()
    }

    override fun onDraw(canvas: Canvas?) {
        mBgRectF.set(0F, 0F, measuredWidth.toFloat(), measuredHeight.toFloat())
        canvas?.let {
            canvas.drawRoundRect(mBgRectF, mCorner, mCorner, mPaint)
        }
        super.onDraw(canvas)
    }
}